package Statistics;

import java.util.ArrayList;
import java.util.Collections;

import Definitions.GraphClass;
import Definitions.NodeClass;


public class richCLubCoefficientsClass {
	
	class richClubNodesClass implements Comparable<richClubNodesClass>
	{
		NodeClass oneRichNode;
		int numberOfConnectionsToRichNeighbors;				
		
		richClubNodesClass(NodeClass newRichNode)
		{
			oneRichNode = newRichNode;
			numberOfConnectionsToRichNeighbors = 0;			
		}
		
		public int compareTo(richClubNodesClass richNodeContainer)
		{
			return oneRichNode.getNumberOfNeigbours(edgeType);			
		}		
	}
	
	ArrayList <richClubNodesClass> richCLubNodesArray;
	double richClubCoefficient;
	int edgeType;
	int threshold;
	GraphClass graph;
	
	
	public richCLubCoefficientsClass(GraphClass graph, int threshold, int edgeType)
	{
		this.graph = graph;
		this.threshold =threshold;
		this.edgeType = edgeType;
		
		richCLubNodesArray =  new ArrayList<richClubNodesClass>();
	}
	
	public void printRichClubCoefficientOfTheGraph(GraphClass graph)
	{

		int richConnections=0;	//, allConnections=0;
		
		for(int i=0; i<richCLubNodesArray.size(); i++)
		{
			richConnections += richCLubNodesArray.get(i).numberOfConnectionsToRichNeighbors;
		}
		System.out.println("richConnectionsInPrint:"+richConnections);	//+" allConnections:"+allConnections);		
	}
	
	void calculateRichClubCoefficientsForTheGivenThreshold()
	{	
		ArrayList <NodeClass> dataSetOfTheGraph = graph.getDataSet();
		
		for(int i=0, k=0; i<dataSetOfTheGraph.size(); i++)
   	  	{
			if(dataSetOfTheGraph.get(i).getNumberOfNeigbours(edgeType) >= threshold)
			{				
				richCLubNodesArray.add(new richClubNodesClass(dataSetOfTheGraph.get(i)));
				k++;
			}
   		}
		
		//System.out.println("Zengin zuppe sayisi:"+ richCLubNodesArray.size());
		
		for(int i=0; i<richCLubNodesArray.size(); i++)
		{
			for(int j=i; j<richCLubNodesArray.size(); j++)
			{				
				if(NodeClass.isThereAnyEdge(richCLubNodesArray.get(i).oneRichNode, richCLubNodesArray.get(j).oneRichNode, edgeType))
				{					
					richCLubNodesArray.get(i).numberOfConnectionsToRichNeighbors++;
					//System.out.println(richCLubNodesArray.get(i).oneRichNode.getId()+" ile "+richCLubNodesArray.get(j).oneRichNode.getId()+ "arasinda link var.");
				}				
			}
		}
	}
	
	double calculateRichClubCoefficientOfTheGraph()
	{
		int richConnections=0;		
				
		for(int i=0; i<richCLubNodesArray.size(); i++)
		{
			richConnections += richCLubNodesArray.get(i).numberOfConnectionsToRichNeighbors;						
		}
		
		if(richCLubNodesArray.size()<=1 || richConnections==0)
			return 0;
		
		richClubCoefficient = (double)2*richConnections/(richCLubNodesArray.size()*(richCLubNodesArray.size()-1));
		
		//System.out.println("richClubCoefficientInCalculate:"+richClubCoefficient);
		
		return richClubCoefficient;		
	}
	
	public void sort()
	{		
		Collections.sort(richCLubNodesArray);		
	}
}
